package com.wlyuan.open.infrastructure.jobhandler;

import com.wlyuan.open.application.service.PushService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * 推送服务
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class PushByIdJobHandler extends IJobHandler{

    private final PushService pushService;

    public void handle() {
        String jobParam = XxlJobHelper.getJobParam();
        if(StringUtils.isNotBlank(jobParam)){
            List<Long> taskIds = new ArrayList<>();
            String[] jobParams = jobParam.split(",");
            for(String taskId:jobParams){
                taskIds.add(Long.parseLong(taskId));
            }
            pushService.push(taskIds);
        }

    }

    @Override
    @XxlJob("PushByIdJob")
    public void execute() {
        XxlJobHelper.log("创建根据id推送数据任务...");
        this.handle();
        XxlJobHelper.log("创建根据id推送数据完成...");
        XxlJobHelper.handleSuccess();
    }

}
